//
//  DMMyOrderCommentVC.m
//  DistributionMallApp
//
//  Created by JHnoplan on 2018/10/19.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMMyOrderCommentVC.h"
//view
#import "KChooseImageControlView.h"
//request
#import "KUploadImageRequest.h"
#import "KStoreRequest.h"



@interface DMMyOrderCommentVC ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *titleLb;
@property (weak, nonatomic) IBOutlet UILabel *secondLb;
@property (weak, nonatomic) IBOutlet UILabel *priceLb;
@property (weak, nonatomic) IBOutlet UILabel *numLb;
@property (weak, nonatomic) IBOutlet UITextView *contentTV;
@property (weak, nonatomic) IBOutlet UILabel *placeholderLb;
@property (weak, nonatomic) IBOutlet UIImageView *firstImage;
@property (weak, nonatomic) IBOutlet UIImageView *secondImage;
@property (weak, nonatomic) IBOutlet UIImageView *thirdImage;
@property (weak, nonatomic) IBOutlet UIImageView *fourthImage;
@property (weak, nonatomic) IBOutlet UIView *starView;

@property (assign, nonatomic)  NSInteger score;

@property (nonatomic,strong) NSArray * imageListArr;
@property (weak, nonatomic) IBOutlet UIButton *goodBtn;
@property (weak, nonatomic) IBOutlet UIButton *midBtn;
@property (weak, nonatomic) IBOutlet UIButton *badBtn;

@property (nonatomic,strong) UIButton * selectBtn;

@end

@implementation DMMyOrderCommentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    
    self.navigationItem.title = @"发表评价";
    self.view.backgroundColor = DMDefaultColor;
    _contentTV.delegate = self;
    [self selectLevelBtnAction:self.goodBtn];

    //评分
    for (int i = 0; i < 5; i++) {

                        
    UIButton * scoreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    scoreBtn.frame =CGRectMake(i*20, 0, 18, 14);
        [scoreBtn setImage:[UIImage imageNamed:@"love_hui"] forState:UIControlStateNormal];
        [scoreBtn setImage:[UIImage imageNamed:@"love"] forState:UIControlStateSelected];
        scoreBtn.tag = i+1;
        [scoreBtn addTarget:self action:@selector(scoreSelectActionWithBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_starView addSubview:scoreBtn];
        if (i == 4) {
            [self scoreSelectActionWithBtn:scoreBtn];
        }
    }
    
    //上传图片
    self.imageListArr = @[_firstImage, _secondImage, _thirdImage, _fourthImage];
    for (int i = 0; i < self.imageListArr.count; i++) {
        UIImageView *iv = self.imageListArr[i];
        iv.userInteractionEnabled = YES;
        iv.tag = 100+i;
        [iv addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)]];
    }
    
    if (self.model.soItemViews.count>0) {
        KDisOrderItemModel * itemModel = self.model.soItemViews[0];
        self.titleLb.text = itemModel.name;
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:itemModel.mainCloudPath] placeholderImage:[UIImage imageNamed:@"moren_touxiang"]];
        self.secondLb.text = [NSString stringWithFormat:@"规格:%@",itemModel.attrOpValues];


    }
    self.numLb.text = [NSString stringWithFormat:@"共%@件",self.model.prodQty];
    self.priceLb.text =[NSString stringWithFormat:@"¥%@",self.model.total];
}

-(void)tapClick:(UITapGestureRecognizer *)tap {
    [_contentTV resignFirstResponder];
    
    //头像
    MJWeakSelf
    [KChooseImageControlView showChooseImageControllerWithBaseVC:self imageCount:1 SureBlock:^(NSArray<UIImage *> *imageArr) {
        
        if (imageArr.count>0) {
            NSInteger i = tap.view.tag-100;
            switch (i) {
                case 0:
                    weakSelf.firstImage.image = imageArr[0];
                    break;
                case 1:
                    weakSelf.secondImage.image = imageArr[0];
                    break;
                case 2:
                    weakSelf.thirdImage.image = imageArr[0];
                    break;
                case 3:
                    weakSelf.fourthImage.image = imageArr[0];
                    break;
                    
                default:
                    break;
            }
        }
    }];
}




-(void)scoreSelectActionWithBtn:(UIButton *)btn{
    self.score = btn.tag;
    for (UIButton * subBtn in self.starView.subviews) {
        subBtn.selected = subBtn.tag<=btn.tag;
    }
}

- (IBAction)selectLevelBtnAction:(UIButton *)sender {
    
    if (self.selectBtn == sender) {
        return;
    }
    sender.selected = YES;
    self.selectBtn.selected = NO;
    self.selectBtn = sender;
}

#pragma mark ----netWork----
//确认评价
- (IBAction)submitClick:(UIButton *)sender {
    [JLProgressHUDManager showLoadingHud];
    NSMutableArray * picsUrlArr = [NSMutableArray array];
    //获取单张图片并上传
    dispatch_group_t group = dispatch_group_create();
    __block BOOL isUploadFail = NO;
    for (UIImageView * imageView in self.imageListArr) {
        UIImage * pic = imageView.image;
        //上传图片
        dispatch_group_enter(group);
        
        KUploadImageRequest * request = [[KUploadImageRequest alloc]initWithUploadImage:pic];
        
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            
            NSLog(@"----->上传成功");
            NSDictionary * dic = [NSDictionary dictionaryWithDictionary:baseModel.data];
            
            NSString * imageURL = [dic objectForKey:@"imageUrl"];
            [picsUrlArr addObject:imageURL];
            
            dispatch_group_leave(group);
            
        } failure:^(KBaseModel *errorModel) {
            
            isUploadFail = YES;
            dispatch_group_leave(group);
        }];
        
    }
    
    MJWeakSelf
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        [JLProgressHUDManager hideHud];
        if (isUploadFail) {
            [JLProgressHUDManager showErrorHud:@"上传失败,请重新上传"];
            return ;
        }
        
        NSString * picsStr = [picsUrlArr componentsJoinedByString:@","];
        
        NSString * evaType;
        if (self.selectBtn == self.goodBtn) {
            evaType = @"good_review";
        }else if (self.selectBtn == self.midBtn) {
            evaType = @"middle_review";
        }else{
            evaType = @"mistake_review";
            
        }
        
        KStoreRequest * request = [[KStoreRequest alloc]initUploadProductEvaluateWithContent:weakSelf.contentTV.text imgUrl:picsStr score:@(weakSelf.score).stringValue soId:weakSelf.model.id type:evaType];
        
        [JLProgressHUDManager showLoadingHud];
        MJWeakSelf
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            [JLProgressHUDManager showSuccessHud:@"评价成功"];
            if (weakSelf.reloadBlock) {
                weakSelf.reloadBlock();
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            
        } failure:^(KBaseModel *errorModel) {
            
            [JLProgressHUDManager showErrorHud:errorModel.msg];
        }];
    });
}



-(void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length == 0) {
        _placeholderLb.hidden = NO;
    }else {
        _placeholderLb.hidden = YES;
    }
}

@end
